﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ControlGenerator
{
    /// <summary>
    /// Generates code for a given RoutedEvent
    /// </summary>
    class RoutedEventGenerator
    {
        CSharpCodeWriter codeWriter;

        public RoutedEventGenerator(CSharpCodeWriter codeWriter)
        {
            this.codeWriter = codeWriter;
        }

        public void GenerateRoutedEvent(RoutedEventConfiguration routedEvent)
        {
            this.codeWriter.WriteComment();
            this.codeWriter.WriteComment("{0} RoutedEvent", routedEvent.Name);
            this.codeWriter.WriteComment();

            WriteRoutedEvent(routedEvent);

            WriteDotNetEvent(routedEvent);

        }

        void WriteRoutedEvent(RoutedEventConfiguration routedEvent)
        {
            string initStatement = String.Format("EventManager.RegisterRoutedEvent(\"{0}\",RoutingStrategy.{1},typeof({2}),typeof({3}))",
                                                 routedEvent.Name,
                                                 routedEvent.RoutingStrategy,
                                                 routedEvent.HandlerType,
                                                 routedEvent.ControlConfiguration.Name);

            this.codeWriter.WriteSummaryComment("Identifies the {0} RoutedEvent.", routedEvent.Name);
            this.codeWriter.WriteStatement("public static readonly RoutedEvent {0} = {1}",
                                            routedEvent.RoutedEventName,
                                            initStatement);

            this.codeWriter.WriteLine();
        }

        void WriteDotNetEvent(RoutedEventConfiguration routedEvent)
        {
            codeWriter.WriteSummaryComment(routedEvent.Summary);
            if (false == String.IsNullOrEmpty(routedEvent.Remarks))
            {
                codeWriter.WriteRemarksComment(routedEvent.Remarks);
            }
            codeWriter.WriteDotNetEventWrapper(routedEvent.Name, routedEvent.RoutedEventName, routedEvent.HandlerType);

            codeWriter.WriteLine();
        }
    }
}
